package com.heatup.apt.management.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.heatup.apt.management.service.channer.CourseNoticeInfoService;
import com.heatup.apt.model.channer.CourseNoticeInfo;

/**
 *
 * CourseNoticeInfo 控制层
 *
 */
@Controller
@RequestMapping("system/course_notice")
public class CourseNoticeInfoController extends BaseController {

	@Autowired
	private CourseNoticeInfoService courseNoticeInfoService;

	@RequestMapping("/to_notice_list")
	public ModelAndView to_course_notice_list() {
		ModelAndView model = new ModelAndView("operationsmanagement/course_notice_list");
		return model;
	}

	@RequestMapping("/course_notice_list")
	public void course_notice_list(HttpServletResponse response, @RequestParam int pageIndex,
			@RequestParam int pageSize) {

		if (pageIndex > 0)
			--pageIndex;
		if (pageSize <= 0)
			pageSize = 10;

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Page<CourseNoticeInfo> courseNoticePages = new Page<CourseNoticeInfo>(pageIndex, pageSize);
		EntityWrapper<CourseNoticeInfo> entityWrapper = new EntityWrapper<CourseNoticeInfo>();
		courseNoticePages = courseNoticeInfoService.queryCourseNoticeInfoPages(courseNoticePages);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("course_notice_list", courseNoticePages.getRecords());
		jsonObject.put("total_count", courseNoticePages.getTotal());
		responseSendMsg(response, jsonObject.toString());
	}

	@RequestMapping("/to_notice_add")
	public ModelAndView to_course_notice_add() {
		ModelAndView model = new ModelAndView("operationsmanagement/course_notice_add");
		return model;
	}

	@ResponseBody
	@RequestMapping(value = "/course_notice_add", method = { RequestMethod.GET, RequestMethod.POST })
	public String save_course_notice(CourseNoticeInfo courseNoticeInfo) {
		Integer userid = this.getUserInfo().getId();
		if (courseNoticeInfo.getId() != null) {
			courseNoticeInfo.setUpdateBy(userid);
			courseNoticeInfo.setUpdateTime(new Date());
		} else {
			courseNoticeInfo.setCreateBy(userid);
			courseNoticeInfo.setCreateTime(new Date());
		}
		boolean result = courseNoticeInfoService.saveCourseNoticeInfo(courseNoticeInfo);
		if (result) {
			return "success";
		} else {
			return "请等待最近的课程通知发送完成后再新增新的课程通知";
		}
	}

	@ResponseBody
	@RequestMapping(value = "/course_notice_close")
	public String course_notice_close(@RequestParam int id) {
		int userId = this.getUserInfo().getId();
		boolean result = courseNoticeInfoService.updateCourseNoticeStatusInfo(id, userId);
		if (result) {
			return "success";
		} else {
			return "error";
		}

	}

	@ResponseBody
	@RequestMapping(value = "to_course_notice_edit")
	public ModelAndView to_course_notice_edit(@RequestParam int id) {
		ModelAndView model = new ModelAndView("operationsmanagement/course_notice_edit");
		CourseNoticeInfo courseNoticeInfo = courseNoticeInfoService.queryCourseNoticeInfoById(id);

		if (courseNoticeInfo != null) {
			model.addObject("sendtime",
					new SimpleDateFormat("yyyy-MM-dd HH:mm").format(courseNoticeInfo.getSendtime()));
			model.addObject("courseNoticeInfo", courseNoticeInfo);
		}

		return model;
	}

}